package com.room.search.web.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.room.search.IntegrationTest;
import com.room.search.domain.RoomSearchCommunity;
import com.room.search.repository.RoomSearchCommunityRepository;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import javax.persistence.EntityManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

/**
 * Integration tests for the {@link RoomCommunitySearchResource} REST controller.
 */
@IntegrationTest
@AutoConfigureMockMvc
@WithMockUser
class RoomSearchCommunityResourceIT {

    private static final Long DEFAULT_COMMUNITY_ID = 1L;
    private static final Long UPDATED_COMMUNITY_ID = 2L;

    private static final Integer DEFAULT_AVERAGE = 1;
    private static final Integer UPDATED_AVERAGE = 2;

    private static final String ENTITY_API_URL = "/api/room-community-searches";
    private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";

    private static Random random = new Random();
    private static AtomicLong count = new AtomicLong(random.nextInt() + (2 * Integer.MAX_VALUE));

    @Autowired
    private RoomSearchCommunityRepository roomSearchCommunityRepository;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restRoomCommunitySearchMockMvc;

    private RoomSearchCommunity roomSearchCommunity;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static RoomSearchCommunity createEntity(EntityManager em) {
        RoomSearchCommunity roomSearchCommunity = new RoomSearchCommunity().communityId(DEFAULT_COMMUNITY_ID).average(DEFAULT_AVERAGE);
        return roomSearchCommunity;
    }

    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static RoomSearchCommunity createUpdatedEntity(EntityManager em) {
        RoomSearchCommunity roomSearchCommunity = new RoomSearchCommunity().communityId(UPDATED_COMMUNITY_ID).average(UPDATED_AVERAGE);
        return roomSearchCommunity;
    }

    @BeforeEach
    public void initTest() {
        roomSearchCommunity = createEntity(em);
    }

    @Test
    @Transactional
    void createRoomCommunitySearch() throws Exception {
        int databaseSizeBeforeCreate = roomSearchCommunityRepository.findAll().size();
        // Create the RoomCommunitySearch
        restRoomCommunitySearchMockMvc
            .perform(
                post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(roomSearchCommunity))
            )
            .andExpect(status().isCreated());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeCreate + 1);
        RoomSearchCommunity testRoomSearchCommunity = roomSearchCommunityList.get(roomSearchCommunityList.size() - 1);
        assertThat(testRoomSearchCommunity.getCommunityId()).isEqualTo(DEFAULT_COMMUNITY_ID);
        assertThat(testRoomSearchCommunity.getAverage()).isEqualTo(DEFAULT_AVERAGE);
    }

    @Test
    @Transactional
    void createRoomCommunitySearchWithExistingId() throws Exception {
        // Create the RoomCommunitySearch with an existing ID
        roomSearchCommunity.setId(1L);

        int databaseSizeBeforeCreate = roomSearchCommunityRepository.findAll().size();

        // An entity with an existing ID cannot be created, so this API call must fail
        restRoomCommunitySearchMockMvc
            .perform(
                post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(roomSearchCommunity))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeCreate);
    }

    @Test
    @Transactional
    void getAllRoomCommunitySearches() throws Exception {
        // Initialize the database
        roomSearchCommunityRepository.saveAndFlush(roomSearchCommunity);

        // Get all the roomCommunitySearchList
        restRoomCommunitySearchMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(roomSearchCommunity.getId().intValue())))
            .andExpect(jsonPath("$.[*].communityId").value(hasItem(DEFAULT_COMMUNITY_ID.intValue())))
            .andExpect(jsonPath("$.[*].average").value(hasItem(DEFAULT_AVERAGE)));
    }

    @Test
    @Transactional
    void getRoomCommunitySearch() throws Exception {
        // Initialize the database
        roomSearchCommunityRepository.saveAndFlush(roomSearchCommunity);

        // Get the roomCommunitySearch
        restRoomCommunitySearchMockMvc
            .perform(get(ENTITY_API_URL_ID, roomSearchCommunity.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(roomSearchCommunity.getId().intValue()))
            .andExpect(jsonPath("$.communityId").value(DEFAULT_COMMUNITY_ID.intValue()))
            .andExpect(jsonPath("$.average").value(DEFAULT_AVERAGE));
    }

    @Test
    @Transactional
    void getNonExistingRoomCommunitySearch() throws Exception {
        // Get the roomCommunitySearch
        restRoomCommunitySearchMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    void putNewRoomCommunitySearch() throws Exception {
        // Initialize the database
        roomSearchCommunityRepository.saveAndFlush(roomSearchCommunity);

        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();

        // Update the roomCommunitySearch
        RoomSearchCommunity updatedRoomSearchCommunity = roomSearchCommunityRepository.findById(roomSearchCommunity.getId()).get();
        // Disconnect from session so that the updates on updatedRoomCommunitySearch are not directly saved in db
        em.detach(updatedRoomSearchCommunity);
        updatedRoomSearchCommunity.communityId(UPDATED_COMMUNITY_ID).average(UPDATED_AVERAGE);

        restRoomCommunitySearchMockMvc
            .perform(
                put(ENTITY_API_URL_ID, updatedRoomSearchCommunity.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(updatedRoomSearchCommunity))
            )
            .andExpect(status().isOk());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
        RoomSearchCommunity testRoomSearchCommunity = roomSearchCommunityList.get(roomSearchCommunityList.size() - 1);
        assertThat(testRoomSearchCommunity.getCommunityId()).isEqualTo(UPDATED_COMMUNITY_ID);
        assertThat(testRoomSearchCommunity.getAverage()).isEqualTo(UPDATED_AVERAGE);
    }

    @Test
    @Transactional
    void putNonExistingRoomCommunitySearch() throws Exception {
        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();
        roomSearchCommunity.setId(count.incrementAndGet());

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restRoomCommunitySearchMockMvc
            .perform(
                put(ENTITY_API_URL_ID, roomSearchCommunity.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomSearchCommunity))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithIdMismatchRoomCommunitySearch() throws Exception {
        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();
        roomSearchCommunity.setId(count.incrementAndGet());

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomCommunitySearchMockMvc
            .perform(
                put(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomSearchCommunity))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithMissingIdPathParamRoomCommunitySearch() throws Exception {
        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();
        roomSearchCommunity.setId(count.incrementAndGet());

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomCommunitySearchMockMvc
            .perform(
                put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(roomSearchCommunity))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void partialUpdateRoomCommunitySearchWithPatch() throws Exception {
        // Initialize the database
        roomSearchCommunityRepository.saveAndFlush(roomSearchCommunity);

        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();

        // Update the roomCommunitySearch using partial update
        RoomSearchCommunity partialUpdatedRoomSearchCommunity = new RoomSearchCommunity();
        partialUpdatedRoomSearchCommunity.setId(roomSearchCommunity.getId());

        restRoomCommunitySearchMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedRoomSearchCommunity.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRoomSearchCommunity))
            )
            .andExpect(status().isOk());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
        RoomSearchCommunity testRoomSearchCommunity = roomSearchCommunityList.get(roomSearchCommunityList.size() - 1);
        assertThat(testRoomSearchCommunity.getCommunityId()).isEqualTo(DEFAULT_COMMUNITY_ID);
        assertThat(testRoomSearchCommunity.getAverage()).isEqualTo(DEFAULT_AVERAGE);
    }

    @Test
    @Transactional
    void fullUpdateRoomCommunitySearchWithPatch() throws Exception {
        // Initialize the database
        roomSearchCommunityRepository.saveAndFlush(roomSearchCommunity);

        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();

        // Update the roomCommunitySearch using partial update
        RoomSearchCommunity partialUpdatedRoomSearchCommunity = new RoomSearchCommunity();
        partialUpdatedRoomSearchCommunity.setId(roomSearchCommunity.getId());

        partialUpdatedRoomSearchCommunity.communityId(UPDATED_COMMUNITY_ID).average(UPDATED_AVERAGE);

        restRoomCommunitySearchMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedRoomSearchCommunity.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRoomSearchCommunity))
            )
            .andExpect(status().isOk());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
        RoomSearchCommunity testRoomSearchCommunity = roomSearchCommunityList.get(roomSearchCommunityList.size() - 1);
        assertThat(testRoomSearchCommunity.getCommunityId()).isEqualTo(UPDATED_COMMUNITY_ID);
        assertThat(testRoomSearchCommunity.getAverage()).isEqualTo(UPDATED_AVERAGE);
    }

    @Test
    @Transactional
    void patchNonExistingRoomCommunitySearch() throws Exception {
        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();
        roomSearchCommunity.setId(count.incrementAndGet());

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restRoomCommunitySearchMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, roomSearchCommunity.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomSearchCommunity))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithIdMismatchRoomCommunitySearch() throws Exception {
        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();
        roomSearchCommunity.setId(count.incrementAndGet());

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomCommunitySearchMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomSearchCommunity))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithMissingIdPathParamRoomCommunitySearch() throws Exception {
        int databaseSizeBeforeUpdate = roomSearchCommunityRepository.findAll().size();
        roomSearchCommunity.setId(count.incrementAndGet());

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomCommunitySearchMockMvc
            .perform(
                patch(ENTITY_API_URL)
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomSearchCommunity))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the RoomCommunitySearch in the database
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void deleteRoomCommunitySearch() throws Exception {
        // Initialize the database
        roomSearchCommunityRepository.saveAndFlush(roomSearchCommunity);

        int databaseSizeBeforeDelete = roomSearchCommunityRepository.findAll().size();

        // Delete the roomCommunitySearch
        restRoomCommunitySearchMockMvc
            .perform(delete(ENTITY_API_URL_ID, roomSearchCommunity.getId()).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<RoomSearchCommunity> roomSearchCommunityList = roomSearchCommunityRepository.findAll();
        assertThat(roomSearchCommunityList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
